package com.bw.a2208_park_kotlin


import android.os.CountDownTimer
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.common.entity.BannerEntity
import com.bw.a2208_park_kotlin.databinding.ActivitySplashBinding
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.adapter.ImageBannerAdapter
import com.bw.lib_common.view.BaseMvvmActivity
import com.bw.lib_image.GlideImageLoader
import com.tencent.mmkv.MMKV
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.listener.OnPageChangeListener
import com.youth.banner.transformer.AlphaPageTransformer

/**
 * 启动页面
 * 业务功能
 * 1.APP介绍（轮播图片，动画，视频）
 * 2.接入广告（开屏广告）
 * 3.第一次安装只会展示App功能，二次打开就有广告
 * 4.选择兴趣标签（为后期内容做推荐）
 *
 */
class SplashActivity : BaseMvvmActivity<ActivitySplashBinding>() {

    private lateinit var mCountDownTimer: CountDownTimer
    private var bannerData = mutableListOf<BannerEntity>()
    private lateinit var mBannerAdapter: ImageBannerAdapter

    override fun initViews() {
        super.initViews()
        //判断是否为第一次进入
        val isFirstBoot = MMKV.defaultMMKV().decodeBool("isFirstBoot", true)
        if (isFirstBoot){
            MMKV.defaultMMKV().encode("isFirstBoot", false)
            showAppBannerImage()
        }
        //显示5秒倒计时广告
        else{
            showAppAdvLayout()
        }
    }

    override fun observeDataChange() {

    }

    /**
     * 加载闪屏页布局
     */

    private fun showAppAdvLayout(){
        GlideImageLoader.loadResImage(mDataBinding.ivAdv, com.bw.lib_resource.R.drawable.adv_0,mDataBinding.ivAdv)
        mDataBinding.clAppAdv.visibility = View.VISIBLE
        mDataBinding.clAppBanner.visibility = View.INVISIBLE

        //Glide加载闪屏页大图
        mDataBinding.ivAdv.run{
            GlideImageLoader.loadResImage(this, com.bw.lib_resource.R.drawable.adv_0,this)
        }

        //5秒倒计时
        mCountDownTimer = object : CountDownTimer(5000, 1000){
            override fun onTick(millisUntilFinished: Long) {
                mDataBinding.tvAdvSkip.text = "${millisUntilFinished / 1000}秒跳过"
            }

            override fun onFinish() {
                this@SplashActivity.jumpToMainPage()
            }
        }

        //启动倒计时
        mCountDownTimer.start()

        mDataBinding.tvAdvSkip.setOnClickListener {
            this@SplashActivity.jumpToMainPage()
        }

    }

    /**
     * 加载引导页布局
     */
    private fun showAppBannerImage(){
        GlideImageLoader.loadResImage(mDataBinding.ivAdv, com.bw.lib_resource.R.drawable.adv_0,mDataBinding.ivAdv)
        mDataBinding.clAppBanner.visibility = View.VISIBLE
        mDataBinding.clAppAdv.visibility = View.INVISIBLE

        //设置Banner数据
        bannerData.run {
            add(BannerEntity(0, com.bw.lib_resource.R.drawable.wellcome_0, ""))
            add(BannerEntity(1, com.bw.lib_resource.R.drawable.wellcome_1, ""))
            add(BannerEntity(2, com.bw.lib_resource.R.drawable.wellcome_2, ""))
        }

        mBannerAdapter = ImageBannerAdapter(bannerData)

        mDataBinding.appBanner.run {
            setAdapter(mBannerAdapter)
            //关联Banner与Activity的生命周期
            addBannerLifecycleObserver(this@SplashActivity)
            //设置指示器的样式
            indicator = CircleIndicator(this@SplashActivity)
            //设置切换效果
            addPageTransformer(AlphaPageTransformer())
            //设置页面切换监听事件
            addOnPageChangeListener(object : OnPageChangeListener {
                override fun onPageScrolled(position: Int,positionOffset: Float, positionOffsetPixels: Int) { }

                override fun onPageSelected(position: Int) {
                    //滑动到最后一页
                    if (position == bannerData.size - 1){
                        mDataBinding.tvStart.visibility = View.VISIBLE
                    }else{
                        mDataBinding.tvStart.visibility = View.INVISIBLE
                    }
                }

                override fun onPageScrollStateChanged(state: Int) { }

            })
        }

        //设置立即进入的点击事件
        mDataBinding.tvStart.setOnClickListener {
            this@SplashActivity.jumpToMainPage()
        }
    }

    private fun jumpToMainPage(){
        ARouter.getInstance().build(RouterPath.PATH_MAIN_PAGE).navigation()
        this@SplashActivity.finish()
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    override fun getLayoutId(): Int =R.layout.activity_splash

}